//
//  HROrderProgressViewController.m
//  haierapp
//
//  Created by gaotangzhang on 14-8-17.
//  Copyright (c) 2014年 haier. All rights reserved.
//

#import "HROrderProgressViewController.h"

#import "HROrderProgressCell.h"

#import "HROrderTracking.h"


#define IDENTIFIER_CELL_PROGRESS @"OrderProgressCell"

@interface HROrderProgressViewController (){
    NSDictionary *_dataSource;
    
    HROrderTracking *_orderTracking;
}

@end

@implementation HROrderProgressViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if (__CUR_IOS_VERSION >= __IPHONE_7_0)
    {
        self.edgesForExtendedLayout= UIRectEdgeNone;
    }
    self.navigationItem.title = @"进度详情";
    
    //隐藏多余的分割线
    UIView *footerView=[[UIView alloc]init];
    footerView.backgroundColor=[UIColor clearColor];
    self.tableView.tableFooterView=footerView;
    
    [self showCustomLoadingHud:kLoading inView:nil];
    
    //加载订单进度
    [HRInterfaceManager getOrderTrackingById:_orderNo completion:^(BOOL isSucceed, NSString *message, id data) {
        [self hideCustomLoadingHud];
        
        if (isSucceed) {
            _orderTracking=(HROrderTracking *)data;
            
            [self.tableView reloadData];
        }else{
            [self showTextHUD:message];
        }
    }];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if (section==0) {
        //订单信息
        return 1;
    }else{
        
        if (_orderTracking!=nil && [_orderTracking.list count]>0) {
            return [_orderTracking.list count];
        }else{
            return 0;
        }
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        NSString *identifierStr=@"orderinfo";
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifierStr];
        
        if (cell==nil) {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierStr];
            
            UILabel *orderIdLabel=[[UILabel alloc]initWithFrame:CGRectMake(13, 13, 300, 15)];
            orderIdLabel.textColor=[UIColor colorFromHexRGB:@"#303030"];
            orderIdLabel.font=[UIFont fontWithName:FONT_NAME_DEFAULT size:14.0f];
            orderIdLabel.tag=10;
            
            UILabel *buyIdLabel=[[UILabel alloc]initWithFrame:CGRectMake(13, 32, 300, 15)];
            buyIdLabel.textColor=[UIColor colorFromHexRGB:@"#303030"];
            buyIdLabel.font=[UIFont fontWithName:FONT_NAME_DEFAULT size:14.0f];
            buyIdLabel.tag=20;
            
            [cell addSubview:orderIdLabel];
            [cell addSubview:buyIdLabel];
            
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
        }
        
        UILabel *orderIdLabel=[cell viewWithTag:10];
        UILabel *buyIdLabel=[cell viewWithTag:20];
        
        orderIdLabel.text=[NSString stringWithFormat:@"订单编号：%@",_orderTracking.orderNo];
        //buyIdLabel.text=[NSString stringWithFormat:@"支付宝交易号：%@",[orderInfo objectForKey:@"dealId"]];
        
        return cell;
        
    }else{
        HROrderProgressCell *cell=[tableView dequeueReusableCellWithIdentifier:IDENTIFIER_CELL_PROGRESS];
        
        if (cell==nil) {
            cell=[[HROrderProgressCell alloc]init];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
        }
        
        HROrderTrackingInfo *trackingInfo=[_orderTracking.list objectAtIndex:indexPath.row];
        
        [cell configWithData:trackingInfo];
        
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return 60.0f;
    }else{
        return 70.0f;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1f;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    if (_orderTracking!=nil && [_orderTracking.list count]>0) {
        return 10.0f;
    }else{
        return 0;
    }
    
    
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
